package ifi.card.joueur;

import ifi.card.poker.Poker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.ifi.client.controller.GameController;

import Definir.DefinirPokey;
import android.app.ListActivity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;

public class Player implements DefinirPokey{
	public List<Poker> pokersOfPlayer;
	public int numPorker;

	public Player() {
		// TODO Auto-generated constructor stub
		pokersOfPlayer = new ArrayList<Poker>();
	}

	public void initPlayer() {
		Random rnd = new Random();
		for (int i = 0; i < 13; i++) {
			Poker poker = new Poker();
			poker.matier = CO_POKER;
			poker.numPoker = i+3;
			pokersOfPlayer.add(poker);
		}
		initPosition();
	}

	public void initPosition(){
		float k = (800 - pokersOfPlayer.size()*60)/2;
		for (int i = 0; i < pokersOfPlayer.size(); i++) {
			Poker poker = pokersOfPlayer.get(i);
			poker.possitionXOrigine = k + i * 60;
			poker.possitionX = poker.possitionXOrigine;
			poker.possitionYOrigine = 395;
			poker.possitionX = poker.possitionXOrigine;
		}
	}

	public void sortPokers(List<Poker> jouer) {
		Collections.sort(jouer);
	}
	
	public List<Poker> selectPoker(int x, int y, List<Poker> pokers) {
		for (int i = 0; i < pokersOfPlayer.size(); i++) {
			if ((pokersOfPlayer.get(i).possitionXOrigine > x - 60)
					&& (pokersOfPlayer.get(i).possitionXOrigine < x) && (y > 380)) {
				pokersOfPlayer.get(i).possitionY = pokersOfPlayer.get(i).possitionYOrigine - 20;

				if (!pokersOfPlayer.get(i).isSelect)
					pokers.add(pokersOfPlayer.get(i));
				pokersOfPlayer.get(i).isSelect = !pokersOfPlayer.get(i).isSelect;
			}
		}
		return pokers;
	}

	public boolean movePoker(int x, int y, List<Poker> pokers) {
		
		for (int i = 0; i < pokers.size(); i++) {
				pokers.get(i).possitionX = x + 60 * i;
				pokers.get(i).possitionY = y;
		}
		return true;
	}

	public void unSelectPoker(List<Poker> pokers, boolean isPlay) {
		GameController c = new GameController();
		int l[] = {3,4,5,6};
		int m[] = {CO_POKER,CO_POKER,CO_POKER,CO_POKER};
		boolean allowed = c.play(pokers, l, m);
		if (!isPlay) {
			
			for (int i = 0; i < pokersOfPlayer.size(); i++) {
				pokersOfPlayer.get(i).isSelect = false;
				pokersOfPlayer.get(i).possitionX = pokersOfPlayer.get(i).possitionXOrigine;
				pokersOfPlayer.get(i).possitionY = pokersOfPlayer.get(i).possitionYOrigine;
			}
		} else {
			if(allowed){
			pokersOfPlayer.removeAll(pokers);
			}
		}
		pokers.clear();
		initPosition();
	}
	
//===  ve bai cua nguoi choi ====================================================
	public void drawAllPokers(Canvas canvas, Bitmap bitmapPocker,
			Bitmap bitmapMatier, Bitmap bitmapJQK) {
		for (int i = 0; i < pokersOfPlayer.size(); i++) {
			if (!pokersOfPlayer.get(i).isSelect) {
				pokersOfPlayer.get(i).drawPoker(canvas, bitmapPocker, bitmapMatier,
						bitmapJQK, pokersOfPlayer.get(i).possitionXOrigine,
						pokersOfPlayer.get(i).possitionYOrigine, pokersOfPlayer.get(i).numPoker,
						pokersOfPlayer.get(i).matier);
			} else {
				pokersOfPlayer.get(i).drawPoker(canvas, bitmapPocker, bitmapMatier,
						bitmapJQK, pokersOfPlayer.get(i).possitionX, pokersOfPlayer.get(i).possitionY,
						pokersOfPlayer.get(i).numPoker, pokersOfPlayer.get(i).matier);
			}
		}
	}
}
